﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.FindArtInfo.Entities;
using System.Data.SqlClient;
using System.Data;
using BusinessLayer.FindArtInfo.Models;

namespace BusinessLayer.FindArtInfo
{
    public class SignaturesBL
    {
        public Signatures GetById(int id)
        {
            Signatures result = new Signatures();
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@Id", id);
            try
            {
                result = adapter.GetList<Signatures>("sp_Signatures_Select_By_Id", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit).FirstOrDefault();
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
            return result;
        }

        public List<SignaturesDetailExtend> GetAll(int pageIndex, int pageSize, int? sort)
        {
            List<SignaturesDetailExtend> result = new List<SignaturesDetailExtend>();
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);            
            adapter.AddParameter("@PageIndex", pageIndex);
            adapter.AddParameter("@PageSize", pageSize);
            adapter.AddParameter("@Sort", sort);
            try
            {
                result = adapter.GetList<SignaturesDetailExtend>("sp_Signatures_Select_All", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
            return result;
        }

        public void GetCount(out int totalRecord)
        {
            totalRecord = 0;
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);            
            var output = adapter.AddParameter("@TotalRecord", SqlDbType.Int, ParameterDirection.Output);
            try
            {
                adapter.ExecuteNonQuery("sp_Signatures_Select_Count", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                totalRecord = (int)output.Value;
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
        }

        public List<Signatures> GetByArtistsId(int artistsId)
        {   
            List<Signatures> result = new List<Signatures>();
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@ArtistsId", artistsId);            
            try
            {
                result = adapter.GetList<Signatures>("sp_Signatures_Select_By_ArtistsId", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
            return result;
        }

        public List<ArtistsDetailExtend> GetByLastNameLetter(string letter, int pageIndex, int pageSize)
        {
            List<ArtistsDetailExtend> result = new List<ArtistsDetailExtend>();
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@Letter", letter);
            adapter.AddParameter("@PageIndex", pageIndex);
            adapter.AddParameter("@PageSize", pageSize);
            try
            {
                result = adapter.GetList<ArtistsDetailExtend>("sp_Signatures_Select_By_LastNameLetter", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
            return result;
        }

        public void GetCountByLastNameLetter(string letter, out int totalRecord)
        {
            totalRecord = 0;
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@Letter", letter);
            var output = adapter.AddParameter("@TotalRecord", SqlDbType.Int, ParameterDirection.Output);
            try
            {
                adapter.ExecuteNonQuery("sp_Signatures_Select_Count_By_LastNameLetter", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                totalRecord = Convert.ToInt32(output.Value);
            }
            catch (Exception)
            {
            }
        }
    }
}
